//
//  XMGPresentationController.swift
//  weibo
//
//  Created by wang_h on 2017/5/26.
//  Copyright © 2017年 wang_h. All rights reserved.
//

import UIKit

class XMGPresentationController: UIPresentationController {
    
    var presentedFrame = CGRect.zero
    //MARK:- 懒加载
    lazy var coverView : UIView = UIView()
    
    //MARK:- 系统回调函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        //1.设置弹出View尺寸
        presentedView?.frame = presentedFrame
        
        //2.添加蒙版
        setupCoverView()
    }
}

extension XMGPresentationController {
    func setupCoverView()  {
        //1.添加蒙版
        containerView?.insertSubview(coverView, at: 0)
        //2.设置蒙版的属性
        containerView?.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        coverView.frame = containerView!.bounds
        
        //3.添加手势
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(coverViewClick))
        containerView?.addGestureRecognizer(tapGes)
    }
}
//MARK:- 事件监听
extension XMGPresentationController {
    func coverViewClick() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}











